package com.example.test.springdemo.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.test.springdemo.common.Result;
import com.example.test.springdemo.domain.AdminRole;
import com.example.test.springdemo.service.AdminRoleListService;
import com.example.test.springdemo.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：吉金武
 * @ClassName AdminRoleController
 * @date ：2022/1/31 - 15:07
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 * 角色controller
 **/
@RestController
@RequestMapping("/role")
public class AdminRoleController {

    @Autowired
    private AdminRoleListService adminRoleListService;

    @GetMapping("/roleInfo")
    public Result roleInfo(){
      List<AdminRole> adminRoleList = adminRoleListService.roleInfo();
      return Result.success(adminRoleList);
    }

    @DeleteMapping("/delRole/{id}")
    public Result delRole(@PathVariable("id") Long id){
        adminRoleListService.delRole(id);
        return Result.success("删除成功");
    }

    @GetMapping("/findRoleList")
    public Result findRoleList(AdminRole adminRole){
        List<AdminRole> list = adminRoleListService.findRoleList(adminRole);
        return Result.success(list);
    }

    /**
     * 添加角色
     * @param adminRole
     * @return
     */
    @PostMapping("/addRole")
    public Result addRole(AdminRole adminRole, HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        DecodedJWT verify = JWTUtils.verify(token);
        //获取登录用户
        String loginName = verify.getClaim("username").asString();
        adminRole.setGmtCreateUser(loginName);
        return Result.success(adminRoleListService.addRole(adminRole));
    }

    @PostMapping("/findByRoleId/{id}")
    public Result findByRoleId(@PathVariable("id") Long id){
        AdminRole adminRole = adminRoleListService.findByRoleId(id);
        return Result.success(adminRole);
    }

}

